﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Xml.Serialization;
using System.IO;

namespace QuickOpen
{
    [Serializable]
    public class Settings
    {
        public Size FormSize
        {
            get { return _formSize; }
            set { _formSize = value; }
        }

        public Settings() { }

        public void Save()
        {
            XmlSerializer ser = new XmlSerializer(this.GetType());
            string path = GetSettingsFilePath();
            
            if (!Directory.Exists(Path.GetDirectoryName(path)))
                Directory.CreateDirectory(Path.GetDirectoryName(path));

            using (FileStream fs = File.Create(path))
                ser.Serialize(fs, this);
        }

        public static Settings Load()
        {
            try
            {
                if (!File.Exists(GetSettingsFilePath()))
                    return new Settings();

                XmlSerializer ser = new XmlSerializer(typeof(Settings));
                using (FileStream fs = File.OpenRead(GetSettingsFilePath()))
                    return (Settings)ser.Deserialize(fs);
            }
            catch
            {
                return new Settings();
            }
        }

        private static string GetSettingsFilePath()
        {
            return Path.Combine(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "OpenQuickAddin"), "Settings.dat");
        }

        private Size _formSize = new Size(564, 457);
    }
}
